﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Implements;
using Cofe.Data.Utils.ThirdParty;

namespace Cofe.Data.Filter
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class FileTypeEntryFilterExpression : 
        IEntryFilterExpression<FileTypeEntryFilter>
    {
        public Expression<Func<CofeSystemInfo, bool>>
            GetQueryExpression(FileTypeEntryFilter entryFilter, IOptionFilter[] options)
        {            
            string containStr = entryFilter.Value
                .Replace(".*", "")
                .Replace("*", "").Replace("?", "");

            Expression<Func<CofeSystemInfo, bool>> exp = null;
            switch (entryFilter.Value)
            {
                case FileTypeEntryFilter.Folder:
                case FileTypeEntryFilter.File :
                    string lookupStr = String.Format(",{0},", Core.PropertyDefinitions.CofeDirDefinitions.BeginId);
                    exp = e => e.DatabaseEntry.SupportDefinitions.Contains(lookupStr);                    
                    
                    if (entryFilter.Value == FileTypeEntryFilter.File)
                        exp = exp.Not();
                    break;
                default:
                    exp = e => e.DatabaseEntry.ParseName.ToLower()
                        .EndsWith("." + entryFilter.Value.ToLower());
                    break;
            }            
            
            if (entryFilter.Negative)
                return exp.Not();
            return exp;
        }

    }
}
